package cn.iocoder.yudao.module.system.controller.admin.productionplandetail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.productionplandetail.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.productionplandetail.ProductionPlanDetailDO;
import cn.iocoder.yudao.module.system.service.productionplandetail.ProductionPlanDetailService;

@Tag(name = "管理后台 - 生产计划详情")
@RestController
@RequestMapping("/system/production-plan-detail")
@Validated
public class ProductionPlanDetailController {

    @Resource
    private ProductionPlanDetailService productionPlanDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建生产计划详情")
    @PreAuthorize("@ss.hasPermission('system:production-plan-detail:create')")
    public CommonResult<Integer> createProductionPlanDetail(@Valid @RequestBody ProductionPlanDetailSaveReqVO createReqVO) {
        return success(productionPlanDetailService.createProductionPlanDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产计划详情")
    @PreAuthorize("@ss.hasPermission('system:production-plan-detail:update')")
    public CommonResult<Boolean> updateProductionPlanDetail(@Valid @RequestBody ProductionPlanDetailSaveReqVO updateReqVO) {
        productionPlanDetailService.updateProductionPlanDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产计划详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:production-plan-detail:delete')")
    public CommonResult<Boolean> deleteProductionPlanDetail(@RequestParam("id") Integer id) {
        productionPlanDetailService.deleteProductionPlanDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生产计划详情")
                @PreAuthorize("@ss.hasPermission('system:production-plan-detail:delete')")
    public CommonResult<Boolean> deleteProductionPlanDetailList(@RequestParam("ids") List<Integer> ids) {
        productionPlanDetailService.deleteProductionPlanDetailListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产计划详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:production-plan-detail:query')")
    public CommonResult<ProductionPlanDetailRespVO> getProductionPlanDetail(@RequestParam("id") Integer id) {
        ProductionPlanDetailDO productionPlanDetail = productionPlanDetailService.getProductionPlanDetail(id);
        return success(BeanUtils.toBean(productionPlanDetail, ProductionPlanDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产计划详情分页")
    @PreAuthorize("@ss.hasPermission('system:production-plan-detail:query')")
    public CommonResult<PageResult<ProductionPlanDetailRespVO>> getProductionPlanDetailPage(@Valid ProductionPlanDetailPageReqVO pageReqVO) {
        PageResult<ProductionPlanDetailDO> pageResult = productionPlanDetailService.getProductionPlanDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductionPlanDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产计划详情 Excel")
    @PreAuthorize("@ss.hasPermission('system:production-plan-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductionPlanDetailExcel(@Valid ProductionPlanDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductionPlanDetailDO> list = productionPlanDetailService.getProductionPlanDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产计划详情.xls", "数据", ProductionPlanDetailRespVO.class,
                        BeanUtils.toBean(list, ProductionPlanDetailRespVO.class));
    }

}
